package javaDemo10.java程序设计作业;

import java.util.*;

public class BankSystem {
    private BankSystem() {
    }

    public static void main() {
        HashMap<String, Account> idmap = new HashMap();
        LinkedList<String> aidlist = new LinkedList();
        LinkedList<String> bidlist = new LinkedList();
        LinkedList<String> cidlist = new LinkedList();
        HashMap<String, LinkedList<String>> namemap = new HashMap<>();

        Account a1 = new Account("a", "001", "001", 500.5);
        Account a2 = new Account("a", "002", "002", 5000.5);
        Account a3 = new Account("a", "003", "003", 50000.5);
        Account b1 = new Account("b", "004", "004", 1200.5);
        Account b2 = new Account("b", "005", "005", 50450.5);
        Account c1 = new Account("c", "006", "006", 50099.5);

        idmap.put(a1.getAccount(), a1);
        idmap.put(a2.getAccount(), a2);
        idmap.put(a3.getAccount(), a3);
        idmap.put(b1.getAccount(), b1);
        idmap.put(b2.getAccount(), b2);
        idmap.put(c1.getAccount(), c1);

        aidlist.add(a1.getAccount());
        aidlist.add(a2.getAccount());
        aidlist.add(a3.getAccount());
        bidlist.add(b1.getAccount());
        bidlist.add(b2.getAccount());
        cidlist.add(c1.getAccount());

        namemap.put(a1.getName(), aidlist);
        namemap.put(b1.getName(), bidlist);
        namemap.put(c1.getName(), cidlist);


        loop(idmap, namemap);

    }

    private static void loop(HashMap<String, Account> idmap, HashMap<String, LinkedList<String>> namemap) {
        Scanner sc = new Scanner(System.in);
        welcome();
        while (true) {
            System.out.println("---------------");
            System.out.println("请输入接下来的指令");
            System.out.println("重新调出操作指示请输入  help ");
            String a = sc.nextLine();
            switch (a) {
                case "1": {
                    one(idmap);
                    break;
                }

                case "2": {
                    two(namemap);
                    break;
                }

                case "3": {
                    three(namemap);
                    break;
                }

                case "4": {
                    four(idmap, namemap);
                    break;
                }

                case "5": {
                    five(idmap, namemap);
                    break;
                }

                case "6": {
                    six(idmap);
                    break;
                }

                case "7": {
                    seven(idmap);
                    break;
                }
                case "8": {
                    System.out.println("关机ing");
                    System.out.println("欢迎下次光临");
                    return;
                }

                case "help": {
                    welcome();
                }
            }
            System.out.println("---------------");
        }
    }

    private static void welcome() {
        System.out.println("----------欢迎使用银行管理系统----------");
        System.out.println("请输入选项来选择您要使用的功能");
        System.out.println("1 : 使用账户密码登录");
        System.out.println("2 : 查询单个客户信息");
        System.out.println("3 : 查询所以客户信息");
        System.out.println("4 : 查询客户名下单个账户");
        System.out.println("5 : 查询客户名下多个账户");
        System.out.println("6 : 存款");
        System.out.println("7 : 取款");
        System.out.println("8 : 退出");
    }

    private static void one(HashMap<String, Account> idmap) {
        System.out.println("使用账户密码登录如下: ");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账户");
        String acc = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        Set<String> strs = idmap.keySet();
        boolean a = true;
        boolean b = true;
        for (String key : strs) {
            if (acc.equals(key)) {
                a = false;
                if (password.equals(idmap.get(key).getPassword())) {
                    b = false;
                    System.out.println(idmap.get(key));
                }
            }
        }
        if (a) {
            System.out.println("未查询到该账户，请核实后重试");
        } else if (b) {
            System.out.println("密码错误，请核实后重试");
        }
    }

    private static void two(HashMap<String, LinkedList<String>> namemap) {
        System.out.println("查询单个客户信息如下: ");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入客户姓名");
        String name = sc.nextLine();
        if (namemap.containsKey(name)) {
            LinkedList<String> idlist = namemap.get(name);
            System.out.println("姓名\t" + "名下账户");
            System.out.print(name);

            for (String id : idlist) {
                System.out.println("\t" + id);
            }
        } else {
            System.out.println("未查询该用户，请核实后重试");
        }
    }

    private static void three(HashMap<String, LinkedList<String>> namemap) {
        System.out.println("查询所有客户信息如下: ");
        Set<String> names = namemap.keySet();
        for (String name : names) {
            LinkedList<String> idlist = namemap.get(name);
            System.out.println("姓名\t" + "名下账户");
            System.out.print(name);

            for (String id : idlist) {
                System.out.println("\t" + id);
            }
        }
    }

    private static void four(HashMap<String, Account> idmap, HashMap<String, LinkedList<String>> namemap) {
        System.out.println("查询客户名下单个账户如下: ");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入客户姓名");
        String name = sc.nextLine();
        System.out.println("请输入该客户名下账户");
        String acc = sc.nextLine();
        boolean boo = true;
        if (namemap.containsKey(name)) {
            LinkedList<String> idlist = namemap.get(name);
            for (String id : idlist) {
                if (acc.equals(id)) {
                    System.out.println(idmap.get(acc));
                    boo = false;
                }
            }
            if (boo) {
                System.out.println("账户输入错误，请核实后重试");
            }
        } else {
            System.out.println("查无此人，请核实后重试");
        }
    }

    private static void five(HashMap<String, Account> idmap, HashMap<String, LinkedList<String>> namemap) {
        System.out.println("查询客户名下多个账户如下: ");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入查询的客户的姓名");
        String name = sc.nextLine();
        if (namemap.containsKey(name)) {
            LinkedList<String> idlist = namemap.get(name);
            for (String id : idlist) {
                System.out.println(idmap.get(id));
            }
        } else {
            System.out.println("查无此人，请核实后重试");
        }

    }

    private static void six(HashMap<String, Account> idmap) {
        System.out.println("存款如下: ");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账户");
        String id = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        if (idmap.containsKey(id)) {
            if (idmap.get(id).getPassword().equals(password)) {
                System.out.println("请输入存款金额");
                double dou = sc.nextDouble();
                idmap.get(id).setMoney(idmap.get(id).getMoney() + dou);
            } else {
                System.out.println("密码错误，请重新输入");
            }
        } else {
            System.out.println("账户输入错误，请重新输入");
        }
    }

    private static void seven(HashMap<String, Account> idmap) {
        System.out.println("取款如下: ");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账户");
        String id = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        if (idmap.containsKey(id)) {
            if (idmap.get(id).getPassword().equals(password)) {
                System.out.println("请输入取款金额");
                double dou = sc.nextDouble();
                if (dou > idmap.get(id).getMoney()) {
                    System.out.println("取款失败，账户余额不足");
                } else {
                    idmap.get(id).setMoney(idmap.get(id).getMoney() - dou);
                }
            } else {
                System.out.println("密码错误，请重新输入");
            }
        } else {
            System.out.println("账户输入错误，请重新输入");
        }
    }

}
